import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textVex;
	private JTextField textNVex;
	private JComboBox<MGraph> comboBoxGraphs;
	private JLabel lblResult;
	private JTextArea textGraph;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("Graph");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 709, 627);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		comboBoxGraphs = new JComboBox<MGraph>();
		comboBoxGraphs.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				comboBoxGraphsChangedCall();
			}
		});
		comboBoxGraphs.setBounds(14, 12, 563, 24);
		contentPane.add(comboBoxGraphs);

		JButton btnCreate = new JButton("Create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnCreateCall();
			}
		});
		btnCreate.setBounds(591, 11, 98, 27);
		contentPane.add(btnCreate);

		textVex = new JTextField();
		textVex.setBounds(77, 374, 263, 24);
		contentPane.add(textVex);
		textVex.setColumns(10);

		JLabel lblVex = new JLabel("Vex:");
		lblVex.setHorizontalAlignment(SwingConstants.RIGHT);
		lblVex.setBounds(14, 377, 49, 18);
		contentPane.add(lblVex);

		JLabel lblArc = new JLabel("nVex:");
		lblArc.setHorizontalAlignment(SwingConstants.RIGHT);
		lblArc.setBounds(363, 377, 49, 18);
		contentPane.add(lblArc);

		textNVex = new JTextField();
		textNVex.setColumns(10);
		textNVex.setBounds(426, 374, 263, 24);
		contentPane.add(textNVex);

		JButton btnLocate = new JButton("Locate");
		btnLocate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnLocateCall();
			}
		});
		btnLocate.setBounds(91, 411, 113, 27);
		contentPane.add(btnLocate);

		JButton btnFirst = new JButton("First");
		btnFirst.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnFirstCall();
			}
		});
		btnFirst.setBounds(295, 411, 113, 27);
		contentPane.add(btnFirst);

		JButton btnNext = new JButton("Next");
		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnNextCall();
			}
		});
		btnNext.setBounds(499, 411, 113, 27);
		contentPane.add(btnNext);

		lblResult = new JLabel("Create graph first!");
		lblResult.setFont(new Font("宋体", Font.PLAIN, 22));
		lblResult.setHorizontalAlignment(SwingConstants.CENTER);
		lblResult.setBounds(14, 451, 675, 128);
		contentPane.add(lblResult);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(14, 49, 675, 307);
		contentPane.add(scrollPane);

		textGraph = new JTextArea();
		textGraph.setEditable(false);
		scrollPane.setViewportView(textGraph);
	}

	protected void btnNextCall() {
		MGraph graph = (MGraph) comboBoxGraphs.getSelectedItem();
		if (graph == null) {
			return;
		}
		if (textVex.getText().length() < 1) {
			lblResult.setText("Input the Vex!");
			return;
		}
		if (textNVex.getText().length() < 1) {
			lblResult.setText("Input the nVex!");
			return;
		}
		try {
			String vexin = textVex.getText();
			int vexinlocate = graph.locateVex(vexin);
			if (vexinlocate < 0) {
				lblResult.setText("Can not find \"" + vexin + "\"!");
				return;
			}
			String nvexin = textNVex.getText();
			int nvexinlocate = graph.locateVex(nvexin);
			if (nvexinlocate < 0) {
				lblResult.setText("Can not find \"" + nvexin + "\"!");
				return;
			}
			int nextvexlocate = graph.nextAdjVex(vexinlocate, nvexinlocate);
			if (nextvexlocate < 0) {
				lblResult.setText("\"" + vexin
						+ "\" has no adjacency point after \"" + nvexin + "\"");
				return;
			}
			String nextvex = (String) graph.getVex(nextvexlocate);
			lblResult.setText(" The adjacency point of \"" + vexin
					+ "\" next to \"" + nvexin + "\" is \"" + nextvex + "\"");
		} catch (Exception e) {
			lblResult.setText(e.getMessage());
			return;
		}
	}

	protected void btnFirstCall() {
		MGraph graph = (MGraph) comboBoxGraphs.getSelectedItem();
		if (graph == null) {
			return;
		}
		if (textVex.getText().length() < 1) {
			lblResult.setText("Input the Vex!");
			return;
		}
		try {
			String vexin = textVex.getText();
			int vexinlocate = graph.locateVex(vexin);
			if (vexinlocate < 0) {
				lblResult.setText("Can not find \"" + vexin + "\"!");
				return;
			}
			int vexfirstlocate = graph.firstAdjVex(vexinlocate);
			if (vexfirstlocate < 0) {
				lblResult.setText("\"" + vexin + "\" has no adjacency point!");
				return;
			}
			String vexfirst = (String) graph.getVex(vexfirstlocate);
			lblResult.setText(" The first adjacency point of \"" + vexin
					+ "\" is \"" + vexfirst + "\"");
		} catch (Exception e) {
			lblResult.setText(e.getMessage());
			return;
		}
	}

	protected void btnLocateCall() {
		MGraph graph = (MGraph) comboBoxGraphs.getSelectedItem();
		if (graph == null) {
			return;
		}
		if (textVex.getText().length() < 1) {
			lblResult.setText("Input the Vex!");
			return;
		}
		int locate = graph.locateVex(textVex.getText());
		if (locate < 0) {
			lblResult.setText("Can not find \"" + textVex.getText() + "\"");
		} else {
			lblResult.setText(
					"Locate \"" + textVex.getText() + "\" at " + locate);
		}
	}

	protected void comboBoxGraphsChangedCall() {
		MGraph graph = (MGraph) comboBoxGraphs.getSelectedItem();
		String string = graph.getString();
		textGraph.setText(string);
		lblResult.setText("Choose an Option!");
	}

	protected void btnCreateCall() {
		CreateDialog dialog = new CreateDialog(this);
		dialog.setVisible(true);
	}

	protected void addGraph(MGraph graph) {
		comboBoxGraphs.addItem(graph);
		comboBoxGraphs.setSelectedItem(graph);
		lblResult.setText("Choose an Option!");
	}
}
